import re
import datetime
from dateutil import relativedelta as rdelta

sample_customer = {
    'id': '00978',
    'first_name': 'Bill',
    'last_name': 'Tong',
    'birthday': '2000-02-02',
    'driver_license': '23333-23333-23333',
    'phone_number': '9053842929'
}

# Required List:
add_customer = ['first_name', 'last_name', 'birthday', 'driver_license', 'phone_number']


def is_date_valid(date):
    res = {'valid': False, 'comment': []}
    try:
        birthday = datetime.datetime.strptime(date, '%Y-%m-%d')
        today = datetime.datetime.now()
        delta = rdelta.relativedelta(today, birthday)
        if delta.years < 18:
            res['comment'].append('Customer age should be greater than 18.')
        elif delta.years > 80:
            res['comment'].append('Customer age should be less than 80.')
        else:
            res['valid'] = True
    except ValueError:
        res['comment'].append('Valid date format is YYYY-MM-DD.')
    return res


def is_license_valid(info):
    # assume license is valid now, but should visit gov api to check
    res = {'valid': True, 'comment': []}
    return res


def is_phone_valid(phone_number):
    res = {'valid': False, 'comment': []}
    if re.match('^\d+$', phone_number) and len(phone_number) <= 32:
        res['valid'] = True
    else:
        res['comment'].append('Phone number should be all digits, and its length should be less than 32.')
    return res


def validate_customer(info):
    res = {'valid': False, 'comment': []}

    missing_list = []
    for key in add_customer:
        if key not in info:
            missing_list.append(key)
    if missing_list:
        res['comment'].append('Missing keys: ' + ', '.join(missing_list))
        return res

    res['comment'] += is_date_valid(info['birthday'])['comment']
    res['comment'] += is_license_valid(info['driver_license'])['comment']
    res['comment'] += is_phone_valid(info['phone_number'])['comment']

    if not res['comment']:
        res['valid'] = True
    return res
